package com.black.cat.jvm.classfile.constantpool.impl;

import com.black.cat.jvm.classfile.ClassReader;
import com.black.cat.jvm.classfile.constantpool.BaseConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantInfo;
import com.black.cat.jvm.classfile.constantpool.ConstantPool;
import com.black.cat.jvm.common.CommonValue;


/**
 * @Author blackcat
 * @create 2021/8/11 20:46
 * @version: 1.0
 * @description:字段或者方法的部分符号引用
 *
 * CONSTANT_NameAndType_info {
 * u1 tag;
 * u2 name_index;
 * u2 descriptor_index;
 * }
 */
public class ConstantInfoNameAndType extends BaseConstantInfo implements ConstantInfo {

    private ConstantPool constantPool;
    private int nameIndex; // 类索引 u2
    private int descriptorIndex;// 方法索引 u2

    public ConstantInfoNameAndType(ConstantPool constantPool, ClassReader reader) {
        this.constantPool = constantPool;
        this.nameIndex = reader.nextU2ToInt();
        this.descriptorIndex = reader.nextU2ToInt();
    }

    @Override
    public String getValue() {
        value = constantPool.getUtfStringByIndex(nameIndex) + ":" + constantPool.getUtfStringByIndex(descriptorIndex);
        return super.getValue();
    }

    @Override
    public int getTag() {
        return CommonValue.CONSTANT_POOL_TAG_NAME_AND_TYPE;
    }
}
